//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Notification;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using System.ComponentModel;

namespace OpenLS.Chart
{
    public class DataLabel : ViewModelBase, IOfficeXmlSerializable, IShape
    {
        private ExtensionElement _extension;
        private bool delete;
        private DataLabelPosition dLblPos;
        private int idx;
        private Layout layout = new Layout();


        private ChartFormat numFmt;
        private string _separator;
        private bool _showBubbleSize;
        private bool _showCatName;
        private bool _showLegendKey;
        private bool _showPercent;
        private bool _showSerName;
        private bool _showVal;

        private ShapeProperties _spPr;
        private Text _tx;

        private TextBody _txPr;

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; notifyPropertyChanged("Extension"); }
        }

        public ChartFormat Format
        {
            get { return numFmt; }
            set { numFmt = value; notifyPropertyChanged("Format"); }
        }

        public ShapeProperties ShapeProperties
        {
            get { return _spPr; }
            set { _spPr = value; notifyPropertyChanged("ShapeProperties"); }
        }

        public TextBody TextProperties
        {
            get { return _txPr; }
            set { _txPr = value; notifyPropertyChanged("TextProperties"); }
        }

        public DataLabelPosition Position
        {
            get { return dLblPos; }
            set { dLblPos = value; notifyPropertyChanged("Position"); }
        }

        public bool LegendKeyVisible
        {
            get { return _showLegendKey; }
            set { _showLegendKey = value; notifyPropertyChanged("LegendKeyVisible"); }
        }

        public bool ValueVisible
        {
            get { return _showVal; }
            set { _showVal = value; notifyPropertyChanged("ValueVisible"); }
        }

        public bool CategoryNameVisible
        {
            get { return _showCatName; }
            set { _showCatName = value; notifyPropertyChanged("CategoryNameVisible"); }
        }

        public bool SeriesNameVisible
        {
            get { return _showSerName; }
            set { _showSerName = value; notifyPropertyChanged("SeriesNameVisible"); }
        }

        public bool PercentVisible
        {
            get { return _showPercent; }
            set { _showPercent = value; notifyPropertyChanged("PercentVisible"); }
        }

        public bool BubbleSizeVisible
        {
            get { return _showBubbleSize; }
            set { _showBubbleSize = value; notifyPropertyChanged("BubbleSizeVisible"); }
        }

        public string Separator
        {
            get { return _separator; }
            set { _separator = value; notifyPropertyChanged("Separator");}
        }


        public Text Text
        {
            get { return _tx; }
            set { _tx = value; notifyPropertyChanged("Text");}
        }


        public Layout Layout
        {
            get { return layout; }
            set { layout = value; notifyPropertyChanged("Layout");}
        }


        public bool Deleted
        {
            get { return delete; }
            set { delete = value; notifyPropertyChanged("Deleted");}
        }

        public int Index
        {
            get { return idx; }
            set { idx = value; notifyPropertyChanged("Index");notifyPropertyChanged("Index");}
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("dLbl"))
            {
                using (ReadContext cc = c.Read("idx"))
                    Index = cc.GetUIntegerAsInteger("val");
                if (c.Reader.LocalName == "delete")
                {
                    using (ReadContext cc = c.Read("delete"))
                        Deleted = cc.GetBoolean("val");
                }
                else
                {
                    Layout = c.ReadOptionalElement<Layout>("layout");
                    _tx = c.ReadOptionalElement<Text>("tx");
                    Format = c.ReadOptionalElement<ChartFormat>("numFmt");
                    ShapeProperties = c.ReadOptionalElement<ShapeProperties>("spPr");
                    TextProperties = c.ReadOptionalElementAs<TextBody>("txPr");
                    Position = c.ReadOptionalElementEnumAttribute("dLblPos", DataLabelPosition.Top);
                        //\\default
                    LegendKeyVisible = c.ReadOptionalElementBooleanAttribute("showLegendKey", false);
                    ValueVisible = c.ReadOptionalElementBooleanAttribute("showVal", false);
                    CategoryNameVisible = c.ReadOptionalElementBooleanAttribute("showCatName", false);
                    SeriesNameVisible = c.ReadOptionalElementBooleanAttribute("showSerName", false);
                    PercentVisible = c.ReadOptionalElementBooleanAttribute("showPercent", false);
                    BubbleSizeVisible = c.ReadOptionalElementBooleanAttribute("showBubbleSize", false);
                    Separator = c.ReadOptionalElementString("separator");
                }
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("dLbl"))
            {
                c.WriteElementUIntegerAttribute("idx", (uint)Index);
                if (Deleted)
                {
                    c.WriteOptionalElementBooleanAttribute("delete", true);
                }
                else
                {
                    c.WriteOptionalElement(Layout);
                    c.WriteOptionalElement(Text);

                    c.WriteOptionalElement(Format);
                    c.WriteOptionalElement(ShapeProperties);
                    c.WriteOptionalElementAs(TextProperties, "txPr");
                    c.WriteOptionalElementEnumAttribute("dLblPos", Position, DataLabelPosition.Top);
                        //\\default
                    c.WriteOptionalElementBooleanAttribute("showLegendKey", LegendKeyVisible);
                    c.WriteOptionalElementBooleanAttribute("showVal", ValueVisible);
                    c.WriteOptionalElementBooleanAttribute("showCatName", CategoryNameVisible);
                    c.WriteOptionalElementBooleanAttribute("showSerName", SeriesNameVisible);
                    c.WriteOptionalElementBooleanAttribute("showPercent", PercentVisible);
                    c.WriteOptionalElementBooleanAttribute("showBubbleSize", BubbleSizeVisible);
                    c.WriteOptionalElementString("separator", Separator);
                }
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

    }
}